<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-11 04:08:14
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-19 20:44:32
 */


namespace api\modules\diandi_shop\controllers;

use api\modules\diandi_shop\services\CommentService;
use Yii;
use app\modules\diandi_shop\models\DdShopComment;
use common\helpers\ErrorsHelper;
use common\helpers\ResultHelper;
use api\controllers\AController;

/**
 * Comment controller for the `DiandiShop` module
 */
class CommentController extends AController
{

    public $modelClass = '\common\models\DdGoods';
    protected $authOptional = ['info', 'list'];

    /**
     * @SWG\Post(path="/diandi_shop/comment/comment",
     *     tags={"商家"},
     *     summary="商家评论",
     *     @SWG\Response(
     *         response = 200,
     *         description = "提交商家评论",
     *         @SWG\Schema(ref = "#/definitions/Docs")
     *     ),
     *     @SWG\Parameter(
     *          in="query",
     *          name="access-token",
     *          type="string",
     *          description="用户秘钥",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="formData",
     *          name="comment",
     *          type="string",
     *          description="评论内容",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="order_id",
     *          type="integer",
     *          description="订单id",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="formData",
     *          name="star_level",
     *          type="integer",
     *          description="评论星级",
     *          required=false,
     *   ),
     *   @SWG\Parameter(
     *          in="formData",
     *          name="images",
     *          type="string",
     *          description="评论图片，多图用逗号隔开",
     *          required=false,
     *   )
     * )
     */
    public function actionComment()
    {

        $DdShopComment = new  DdShopComment();
        $data = Yii::$app->request->post();
        $data['user_id'] = Yii::$app->user->identity->member_id;
        if ($DdShopComment->load($data, '') && $DdShopComment->save()) {
            $id = Yii::$app->db->getLastInsertID();
            return ResultHelper::json(200, '获取成功', ['comment_id' => $id]);
        } else {

            $error = ErrorsHelper::getModelError($DdShopComment);
            return ResultHelper::json(200, $error, []);
        }
    }

    /**
     * @SWG\Post(path="/diandi_shop/comment/list",
     *     tags={"商家"},
     *     summary="获取商家评论",
     *     @SWG\Response(
     *         response = 200,
     *         description = "获取商家评论",
     *         @SWG\Schema(ref = "#/definitions/Docs")
     *     ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="page",
     *          type="string",
     *          description="页码",
     *          required=true,
     *   ),
     *   @SWG\Parameter(
     *          in="query",
     *          name="pageSize",
     *          type="integer",
     *          description="每页显示数量",
     *          required=false,
     *   )
     * )
     */
    public function actionList()
    {
        $pageSize = Yii::$app->request->get('pageSize');
        $list = CommentService::list($pageSize);
        return ResultHelper::json(200, '获取成功', $list);
    }
}
